package com.dingreading.cloud.admin.service.impl;

import com.dingreading.cloud.admin.entity.table.ReaderContestTableDef;
import com.dingreading.cloud.common.util.PageUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import com.dingreading.cloud.admin.entity.ReaderContest;
import com.dingreading.cloud.admin.mapper.ReaderContestMapper;
import com.dingreading.cloud.admin.service.ReaderContestService;
import com.mybatisflex.core.update.UpdateChain;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 *  服务层实现。
 *
 * @author Administrator
 * @since 2024-12-11
 */
@Service
public class ReaderContestServiceImpl extends BaseServiceImpl<ReaderContestMapper, ReaderContest, ReaderContestTableDef> implements ReaderContestService {

    @Override
    protected ReaderContestTableDef getTable() {
        return ReaderContestTableDef.readerContest;
    }

    @Override
    public ReaderContest getByUid(String uid) {
        return getMapper().selectOneByCondition(table.uid.eq(uid));
    }

    @Override
    public Page<ReaderContest> pageList(PageUtil pageUtil, String keyword, Integer type, Integer status) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (status != null) {
            condition.and(table.status.eq(status));
        }
        if (type != null) {
            condition.and(table.type.eq(type));
        }
        if (StringUtils.isNotBlank(keyword)) {
            condition.and(table.storeName.like(keyword)
                    .or(table.addStaffName.like(keyword))
                    .or(table.lessonName.like(keyword))
            );
        }

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.id.desc());

        return getMapper().paginateAs(Page.of(pageUtil.getPage(), pageUtil.getRows()), wrapper, ReaderContest.class);
    }

    @Override
    public boolean updateStatus(String uid, Integer status) {
        return UpdateChain.of(ReaderContest.class)
                .set(table.status, status)
                .where(table.uid.eq(uid))
                .update();
    }
}
